﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;

namespace ChartControl_LiuJinYu
{
    public partial class Form1 : Form
    {
        // Chart控件三个重点集合(相当于Chart控件的子控件):
        // Titles:图表标题集合
        // Series:图表序列集合(坐标系),为展示一个图形确定相应的坐标点集,也可以配置图例类型(曲线、柱状、雷达等)
        // Legends:图表图例集合,代表每个图形的颜色和类型

        public Form1()
        {
            InitializeComponent();

            this.WindowState = FormWindowState.Maximized;

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //Random random = new Random();
            //for (int i = 0; i < 10; i++)
            //{
            // 选对相应的坐标系Series,利用Add方法在这个坐标系的点集合中添加最新的坐标点。
            //    //chart1.Series[0]
            //    // Chart控件Series集合有一个Series,默认的Name属性:Series1
            //    chart1.Series["Series1"].Points.Add(random.Next(0, 101));
            //}
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            for (int i = 1; i <= 4; i++)
            {
                // 参数2:是否搜索所有子控件
                Chart myChart = tableLayoutPanel1.Controls.Find("chart" + i.ToString(), true)[0] as Chart;

                // 只显示最新10个点的数据
                if (myChart.Series[0].Points.Count == 10)
                {
                    myChart.Series[0].Points.RemoveAt(0);
                }
                Random random = new Random();
                int curnum = random.Next(0, 101);
                DataPoint dp1 = new DataPoint();
                dp1.YValues = new double[] { curnum };
                dp1.Label = curnum.ToString();
                myChart.Series["Series1"].Points.Add(dp1);

                // 如果是chart1控件,则在Series2坐标系中也添加一个点。
                if (myChart.Name == "chart1")
                {
                    int curnum2 = random.Next(0, 101);
                    DataPoint dp2 = new DataPoint();
                    dp2.YValues = new double[] { curnum2 };
                    dp2.Label = curnum2.ToString();
                    myChart.Series["Series2"].Points.Add(dp2);
                }
            }



        }
    }
}
